using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using UnityEditor;
using UnityEngine;
using System.ComponentModel;
using UnityEditor.Animations;

namespace Yoozoo.Gameplay.Liberty.AI
{
   public partial class AnimationEventEditor
   {
       public enum AEEditorActionType
       {
           [Description("近战攻击音效")]
           BoxFireSound = 0,
           [Description("近战受击音效")]
           BoxOnHitSound,
           [Description("近战连招1伤害触发")]
           BoxingCombo1,
           [Description("近战连招2伤害触发")]
           BoxingCombo2,
           [Description("近战连招3伤害触发")]
           BoxingCombo3,
           [Description("近战踢伤害触发")]
           BoxingAttackKick,
           [Description("近战向下攻击伤害触发")]
           BoxingAttackDown,
           [Description("手枪伤害触发")]
           HandGunAttack,
           [Description("手枪装弹")]
           HandGunReload,
           [Description("手枪近战伤害触发")]
           HandGunBoxing,
           [Description("近战位移触发")]
           BoxingMove,
           [Description("近战跑动攻击伤害触发")]
           BoxingRunAttack,
           [Description("近战AOE-1段伤害触发")]
           BoxingAOE_1,
           [Description("近战AOE-2段伤害触发")]
           BoxingAOE_2,
           [Description("自动步枪伤害触发")]
           AutoRifleAttack,
           [Description("自动步枪装弹")]
           AutoRifleReload,
           [Description("手雷投掷触发")]
           ThrowGrenade,
           [Description("RPG发射触发")]
           RPGAttack,
           [Description("RPG装弹")]
           RPGReload,
       }
       
       // 遍历获取animator的所有AnimatorState信息
       void browserAnimatorStates(Animator animator, out string[] stateNames, out int[] layers, out string[] showStateNames)
       {
            List<string> stateNameVec = new List<string>();
            List<int> layerVec = new List<int>();
            List<string> showStateNameVec = new List<string>();

            if (animator)
            {
                AnimatorController controller = animator.runtimeAnimatorController as AnimatorController;
                AnimatorOverrideController overrideController = null;
                if (!controller)
                {
                    // 使用AnimatorOverrideController
                    overrideController = animator.runtimeAnimatorController as AnimatorOverrideController;
                    controller = overrideController.runtimeAnimatorController as AnimatorController;
                }

                for (int layer = 0; layer < controller.layers.Length; ++layer)
                {
                    ChildAnimatorState[] childAnimatorStates = controller.layers[layer].stateMachine.states;
                    for (int i = 0; i < childAnimatorStates.Length; ++i)
                    {
                        AnimatorState animatorState = childAnimatorStates[i].state;
                        stateNameVec.Add(animatorState.name);
                        layerVec.Add(layer);
                        showStateNameVec.Add($"{animatorState.name} ({layer})");
                    }

                    // 子状态机
                    ChildAnimatorStateMachine[] childAnimatorStateMachines = controller.layers[layer].stateMachine.stateMachines;
                    foreach (var stateMachine in childAnimatorStateMachines)
                    {
                        childAnimatorStates = stateMachine.stateMachine.states;
                        for (int i = 0; i < childAnimatorStates.Length; ++i)
                        {
                            AnimatorState animatorState = childAnimatorStates[i].state;
                            stateNameVec.Add(animatorState.name);
                            layerVec.Add(layer);
                            showStateNameVec.Add($"{animatorState.name} ({layer})");
                        }
                    }
                }
            }

           stateNames = stateNameVec.ToArray();
           layers = layerVec.ToArray();
           showStateNames = showStateNameVec.ToArray();
       }

       int getSelectedIndex(string id, string[] ids)
       {
           int index = -1;
           if (id != string.Empty && ids != null)
           {
               for (int i = 0; i < ids.Length; ++i)
               {
                   if (id == ids[i])
                   {
                       index = i;
                       break;
                   }
               }
           }

           return index;
       }
       
        // 更新动画帧数
        void UpdateAnimationInfo()
        {
            string stateName = this._SelectedState;
            AnimationClip clip = getAnimationClip(stateName);
            this._AnimationClip = clip;
            this._TotalFrames = getAnimationStateFrames(clip);
            this._AnimationEvents = AnimationEventEditorUtils.GetAnimationEvents(clip);
        }
        
        // 添加Action
        public void addAnimationEventAction(AEEditorActionType actionType)
        {
            switch (actionType)
            {
            case AEEditorActionType.BoxFireSound:
            case AEEditorActionType.BoxOnHitSound:
                {
                    // 添加近战攻击音效事件
                    AnimationEvent evt = new AnimationEvent();
                    evt.time = this._AnimationClip.length * 0.5f;
                    evt.functionName = "OnSoundEvent";
                    switch (actionType)
                    {
                        case AEEditorActionType.BoxFireSound:
                            evt.stringParameter = "sfx_battle_box_fire";
                            break;
                        case AEEditorActionType.BoxOnHitSound:
                            evt.stringParameter = "sfx_battle_box_hit";
                            break;
                    }
                    AnimationEventEditorUtils.AddAnimationEvent(this._AnimationClip, evt);
                    UpdateAnimationInfo();
                }
                break;

            case AEEditorActionType.BoxingMove:
            case AEEditorActionType.BoxingCombo1:
            case AEEditorActionType.BoxingCombo2:
            case AEEditorActionType.BoxingCombo3:
            case AEEditorActionType.BoxingAttackKick:
            case AEEditorActionType.BoxingAttackDown:
            case AEEditorActionType.HandGunBoxing:
            case AEEditorActionType.BoxingRunAttack:
            case AEEditorActionType.BoxingAOE_1:
            case AEEditorActionType.BoxingAOE_2:
                {
                    // 添加近战事件
                    AnimationEvent evt = new AnimationEvent();
                    evt.time = this._AnimationClip.length * 0.5f;
                    evt.functionName = "OnBoxingEvent";
                    switch (actionType)
                    {
                        case AEEditorActionType.BoxingCombo1:   
                            evt.intParameter = (int)AnimationState.BoxingCombo1;
                            break;
                        case AEEditorActionType.BoxingCombo2:
                            evt.intParameter = (int)AnimationState.BoxingCombo2;
                            break;
                        case AEEditorActionType.BoxingCombo3:
                            evt.intParameter = (int)AnimationState.BoxingCombo3;
                            break;
                        case AEEditorActionType.BoxingAttackKick:
                            evt.intParameter = (int)AnimationState.BoxingAttackKick;
                            break;
                        case AEEditorActionType.BoxingAttackDown:
                            evt.intParameter = (int)AnimationState.BoxingAttackDown;
                            break;
                        case AEEditorActionType.HandGunBoxing:
                            evt.intParameter = (int)AnimationState.HandGunBoxing;
                            break;
                        case AEEditorActionType.BoxingMove:
                            evt.intParameter = (int)AnimationState.BoxingMove;
                            break;
                        case AEEditorActionType.BoxingRunAttack:
                            evt.intParameter = (int)AnimationState.BoxingRunAttack;
                            break;
                        case AEEditorActionType.BoxingAOE_1:
                            evt.intParameter = (int)AnimationState.BoxingAOE_1;
                            break;
                        case AEEditorActionType.BoxingAOE_2:
                            evt.intParameter = (int)AnimationState.BoxingAOE_2;
                            break;
                    }
                    AnimationEventEditorUtils.AddAnimationEvent(this._AnimationClip, evt);
                    UpdateAnimationInfo();
                }
                break;

            case AEEditorActionType.HandGunAttack:
            case AEEditorActionType.HandGunReload:
            case AEEditorActionType.AutoRifleAttack:
            case AEEditorActionType.AutoRifleReload:
                {
                    AnimationEvent evt = new AnimationEvent();
                    evt.time = this._AnimationClip.length * 0.5f;
                    evt.functionName = "OnHandGunEvent";
                    switch (actionType)
                    {
                        case AEEditorActionType.HandGunAttack:   
                            evt.intParameter = (int)AnimationState.HandGunShoot;
                            break;
                        case AEEditorActionType.HandGunReload:
                            evt.intParameter = (int)AnimationState.HandGunReload;
                            break;
                        case AEEditorActionType.AutoRifleAttack:
                            evt.intParameter = (int)AnimationState.AutoRifleShoot;
                            break;
                        case AEEditorActionType.AutoRifleReload:
                            evt.intParameter = (int)AnimationState.AutoRifleReload;
                            break;
                    }
                    AnimationEventEditorUtils.AddAnimationEvent(this._AnimationClip, evt);
                    UpdateAnimationInfo();
                }
                break;

            case AEEditorActionType.ThrowGrenade:
                {
                    AnimationEvent evt = new AnimationEvent();
                    evt.time = this._AnimationClip.length * 0.5f;
                    evt.functionName = "OnGrenadeEvent";
                    switch (actionType)
                    {
                        case AEEditorActionType.ThrowGrenade:   
                            evt.intParameter = (int)AnimationState.BombThrow;
                            break;
                    }
                    AnimationEventEditorUtils.AddAnimationEvent(this._AnimationClip, evt);
                    UpdateAnimationInfo();
                }
                break;

            case AEEditorActionType.RPGAttack:
            case AEEditorActionType.RPGReload:
                {
                    AnimationEvent evt = new AnimationEvent();
                    evt.time = this._AnimationClip.length * 0.5f;
                    evt.functionName = "OnRPGEvent";
                    switch (actionType)
                    {
                        case AEEditorActionType.RPGAttack:   
                            evt.intParameter = (int)AnimationState.RPGShoot;
                            break;
                        case AEEditorActionType.RPGReload:   
                            evt.intParameter = (int)AnimationState.RPGReload;
                            break;
                    }
                    AnimationEventEditorUtils.AddAnimationEvent(this._AnimationClip, evt);
                    UpdateAnimationInfo();
                }
                break;
            }
        }
      
   }
   

}

